package com.site.common.config;

import com.site.listener.RedisMessageListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/**
 * Listener监听配置
 */
@Configuration
public class ListenerConfig {

    //===================================== redis监听 start ==================================
    /**
     * redis 监听配置
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        container.setTaskExecutor(iniTaskScheduler());
        container.addMessageListener(new RedisMessageListener(),ChannelTopic.of("RedisTopic"));
        return container;
    }

    /**
     * 创建任务池,运行线程等待处理redis消息
     * @return
     */
    @Bean
    public ThreadPoolTaskScheduler iniTaskScheduler(){
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setPoolSize(20);
        return threadPoolTaskScheduler;
    }
    //===================================== redis监听 end ==================================
}
